define(['app'], function (app) {
    app.controller('BookInfoMngrCtrl', ['$scope', '$state', '$http', '$q', 'infoService',
       function ($scope, $state, $http, $q, infoService) {

           //代码书写步骤
           //1.用【$scope】定义公共类model,和页面绑定的model
           //2.用【var】定义controller内部用的变量
           //3.用【$scope】和【var】定义方法，注意区分两者定义的方法的区别
           //4.最后定义【init】初始化方法，并调用，进行页面的初始化加载
           //定义规则，按可用性分忧优先级，公共的在前，private的在后

           $scope.bookData = [
             { "id": 1, "name": '苏东坡传', "url": "http://s.amazeui.org/media/i/demos/bing-1.jpg", "author": "朱自清" },
             { "id": 2, "name": '小王子', "url": "http://s.amazeui.org/media/i/demos/bing-2.jpg", "author": "" },
             { "id": 3, "name": '富爸爸穷爸爸', "url": "http://s.amazeui.org/media/i/demos/bing-3.jpg", "author": "罗伯特.清崎" },
             { "id": 4, "name": '小王子', "url": "http://s.amazeui.org/media/i/demos/bing-4.jpg", "author": "" },
             { "id": 5, "name": '富爸爸穷爸爸', "url": "http://s.amazeui.org/media/i/demos/bing-1.jpg", "author": "罗伯特.清崎" },
             { "id": 6, "name": '小王子', "url": "http://s.amazeui.org/media/i/demos/bing-2.jpg", "author": "" },
             { "id": 7, "name": '富爸爸穷爸爸', "url": "http://s.amazeui.org/media/i/demos/bing-3.jpg", "author": "罗伯特.清崎" },
             { "id": 8, "name": '小王子', "url": "http://s.amazeui.org/media/i/demos/bing-4.jpg", "author": "" },
             { "id": 9, "name": '富爸爸穷爸爸', "url": "http://s.amazeui.org/media/i/demos/bing-1.jpg", "author": "罗伯特.清崎" }
           ];
           //查询条件
           $scope.filter = {
               name: '',
               author: '',
               publisher: '',
               isbn: '',
               language: ''
           }

           //查询方法
           $scope.search = function () {
               //infoService.searchbook($scope.filter).then(
               //    function (response) {
               //        //if (response.status == 200)
               //        $scope.bookData = response.data;
               //        //else {
               //        //    alert("查询数据失败!");
               //        //}
               //    },
               //    function (error) {
               //        alert("查询数据失败！");
               //    }
               //);

               //if ($scope.filter.name != null && $scope.filter.name!='') {
               //    $filter('filter')($scope.bookData, { name: $scope.filter.name },);
               //}
               //if (true) {

               //}

           };
           //查询方法
           $scope.getAll = function () {
               infoService.getbookAll().then(
                   function (response) {
                       //if (response.status == 200)
                       $scope.bookData = response.data;
                       //else {
                       //    alert("查询数据失败!");
                       //}
                   },
                   function (error) {
                       alert("查询数据失败！");
                   }
               );
           };
           //TODO:取消注释
           //$scope.search();

           //TODO:注释掉查询所有图书的方法
           $scope.reflash = function () {
               $scope.getAll();
           };

           $scope.reflash();
           //添加编辑方法
           $scope.add = function (item, type) {
               //调用添加方法
               if (type == 1) {
                   $state.go("book.infoMngr.edit", { 'type': type });
               }
                   //编辑和查看
               else {
                   $state.go("book.infoMngr.edit", { 'id': item.id, 'type': type });
               }
           }
           //删除方法
           $scope.delete = function (item) {
               if (!confirm("确认删除吗？")) {
                   return;
               }
               infoService.deletebook(item.id).then(
                   function (response) {
                       //if (response.status == 200) {
                       //    alert("删除成功！");
                       $scope.reflash();
                       //}
                       //else
                       //    alert("删除失败！");
                   },
                   function (error) {
                       alert("删除失败！");
                   }
               );
           }
           var init = function () {
               $scope.dashboardModel.isActiveMenu = 'infoMngr';//左侧菜单按钮赋初值
           };
           init();
       }]);
});